/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.conexion;

/**
 *
 * @author andres
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import edu.uoc.tdp.pac4.beans.Perfil;
import edu.uoc.tdp.pac4.comun.GestorConexionInterface;
import edu.uoc.tdp.pac4.beans.Usuari;
import edu.uoc.tdp.pac4.cliente.Principal;
import static edu.uoc.tdp.pac4.cliente.Principal.setContentInternalFrame;
import edu.uoc.tdp.pac4.beans.Taller;
import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;

import java.awt.Color;
import javax.swing.JDesktopPane;

import javax.swing.JInternalFrame;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import edu.uoc.tdp.pac4.conexion.ImmutableTable;
import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author andres
 */
public class GestionConexionListTalleres extends JInternalFrame {

    private GestorConexionInterface conexion;
    private JDesktopPane listTallersWindow;
    private JTable taulaTallers;
    private JScrollPane scrollTaula;
    private JLabel lbl_titol;
    private JButton btn_edit;
    private JButton btn_add;
    
    ArrayList<Taller> llistaTallers;
    ArrayList<Taller> resultatCerca;
    
      private JDesktopPane editUserWindow;
    private JLabel lbl_titol1;
    private JButton btn_update;
    private JButton btn_back;    
    private JButton btn_del;    

    JLabel showAlta ;
    JLabel showModificacio ;                        
    JLabel showBaixa ;
    JTextField txt_nom;                    
    JTextField txt_usuari;
    JPasswordField txt_pass1;
    JPasswordField txt_pass2;    
    JTextField txt_cercar;
    JButton btn_cercar;                        
   ImmutableTable model;    

public  GestionConexionListTalleres(GestorConexionInterface conn) {
    
       
        conexion = conn;

        listTallersWindow=new JDesktopPane();
            
        lbl_titol = new JLabel(TDSLanguageUtils.getMessage("Talleres.Titulo"));
        lbl_titol.setForeground(Color.white);
        lbl_titol.setBounds(200, 40, 200, 40);
        
        /* Capçalera de la taula */
        
        String[] capTaula = {TDSLanguageUtils.getMessage("Talleres.ID"),
            TDSLanguageUtils.getMessage("Talleres.Taller"),
                        TDSLanguageUtils.getMessage("Talleres.Telefono"),
                        TDSLanguageUtils.getMessage("Talleres.Activo"),
                        TDSLanguageUtils.getMessage("Talleres.Borrado")};       
        
        
          
         model = new ImmutableTable(capTaula, 0);
        
         
         
        try {
           ArrayList<Taller> llistaTallers = conexion.getLlistaTallers();
           for(int i = 0; i < llistaTallers.size(); i++) {
                Taller t = llistaTallers.get(i);
                System.out.println(t.getTelefon());
                model.addTaller(t);
           }           
           
           
        txt_cercar = new JTextField();
        txt_cercar.setBounds(200,325, 250, 30 );
           
           
        final JTable taulaTallers = new JTable(model);     
        JScrollPane scrollTaula = new JScrollPane(taulaTallers);
        scrollTaula.setBounds(50,100,500,150);
        
        
        // Afegim el botó nou usuari
        btn_add = new JButton();
        btn_add.setText(TDSLanguageUtils.getMessage("Talleres.NuevoTaller"));
        btn_add.setBounds(40, 450, 150, 40);
        
        // Afegim el botó edició
        
        btn_edit = new JButton();
        btn_edit.setText(TDSLanguageUtils.getMessage("Talleres.Editar"));
        btn_edit.setBounds(200, 450, 150, 40);
        
     
         // Afegim el botó nou taller
        btn_del = new JButton();
        btn_del.setText(TDSLanguageUtils.getMessage("Talleres.Borrar"));
        btn_del.setBounds(360, 450, 150, 40);
        
        
       btn_cercar = new JButton();
       btn_cercar.setText(TDSLanguageUtils.getMessage("Talleres.Buscar"));
       btn_cercar.setBounds(100, 375, 400, 40);
        
        
        /* Listener del botó afegir */
         this.btn_add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent paramAnonymousActionEvent)   {

                    GestionConexionFormTaller g = new GestionConexionFormTaller(conexion);
                    g.nou();
                    g.setVisible(true);
                    g.setBounds(0,0,400,700);
                    Principal.setContentInternalFrame(g,TDSLanguageUtils.getMessage("Talleres.NuevoTaller"));
                    
            };
         });
        
        
        /* Listener del botó editar */
         this.btn_edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent paramAnonymousActionEvent)   {
                int columna = 0;
                int fila = taulaTallers.getSelectedRow();
                
               if(fila<0) {
                   Principal.showError(TDSLanguageUtils.getMessage("Talleres.NoSelectTaller"));
               } else {
                    Object o = taulaTallers.getValueAt(fila, columna);
                    int id = Integer.parseInt(o.toString());
               
                    GestionConexionFormTaller g = new GestionConexionFormTaller(conexion);
                    g.edicio(id);
                    g.setBounds(0,0,400,700);
                    
                    Principal.setContentInternalFrame(g,TDSLanguageUtils.getMessage("Talleres.Editar")); 
                    
                    
               }
                
            };
         });
        

        /* Listener del botó esborrar */
         this.btn_del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent paramAnonymousActionEvent)   {
                
                int columna = 0;
                int fila = taulaTallers.getSelectedRow();
               if(fila<0) {
                   Principal.showError(TDSLanguageUtils.getMessage("Talleres.NoSelectTaller"));
               } else {
                    Object o = taulaTallers.getValueAt(fila, columna);
                    int id = Integer.parseInt(o.toString());
               
                    try {
                    conexion.delTaller(id);
                    } catch(RemoteException e) {
                           e.printStackTrace();
                    } catch (SQLException e) {
                           e.printStackTrace();
                    }
                    
               }
            };
            });

         
            this.btn_cercar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent paramAnonymousActionEvent)   {
                resultatCerca = new ArrayList<Taller>();
                String text = txt_cercar.getText();
                
                try {
                    resultatCerca = conexion.cercarTallers(text);
                   
                    /* Empty table */
                    
                    for(int i = model.getRowCount() - 1; i > -1; i--) {
                        model.removeRow(i);
                    }
                    
                    
                    for(int i = 0; i < resultatCerca.size(); i++) {
                        model.addTaller(resultatCerca.get(i));
                    }
                
                } catch (RemoteException e) {
                    e.printStackTrace();
                }   catch (SQLException e) {
                    e.printStackTrace();
                } 
                
                
                
            };
         });
         
         
         
         
         
       // taula.set
        
         listTallersWindow.add(scrollTaula);
         listTallersWindow.add(lbl_titol);
         listTallersWindow.add(btn_edit);
         listTallersWindow.add(btn_add);
         listTallersWindow.add(btn_del);
         listTallersWindow.add(txt_cercar);
         listTallersWindow.add(btn_cercar);
         this.add(listTallersWindow);
         

         
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
    }
    
    
}
